package com.pzhu.dormitory.controller;

import com.pzhu.dormitory.entity.AbsentFromWorkEntity;
import com.pzhu.dormitory.entity.StudentEntity;
import com.pzhu.dormitory.service.AbsentFromWorkService;
import com.pzhu.dormitory.service.StudentService;
import com.pzhu.dormitory.utils.PageUtils;
import com.pzhu.dormitory.utils.R;
import com.pzhu.dormitory.utils.Rb;
import com.pzhu.dormitory.vo.AbsentFromWorkVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.Optional;


/**
 * @author
 * @email
 * @date 2021-05-01 15:11:29
 */
@Slf4j
@RestController
@Api(value = "AbsentFromWorkController", tags = "")
@RequestMapping("dormitory/absentfromwork")
public class AbsentFromWorkController {
    @Autowired
    private AbsentFromWorkService absentFromWorkService;

    @Autowired
    private StudentService studentService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取list")
    public R<AbsentFromWorkEntity> list(@RequestParam Map<String, Object> params) {
        PageUtils<AbsentFromWorkEntity> page = absentFromWorkService.queryPage(params);
        return Rb.a(AbsentFromWorkEntity.class).success().page(page).build();
    }

    /**
     * 列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "获取list")
    public R<AbsentFromWorkVo> page(@RequestParam Map<String, Object> params) {
        PageUtils<AbsentFromWorkVo> page = absentFromWorkService.queryPageAbsentFromWorkVo(params);
        return Rb.a(AbsentFromWorkVo.class).success().page(page).build();
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "获取一个信息")
    public R<AbsentFromWorkEntity> info(@Validated @PathVariable("id") Long id) {
        AbsentFromWorkEntity absentFromWork = absentFromWorkService.getById(id);
        return Rb.a(AbsentFromWorkEntity.class).success().withData(absentFromWork).build();
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存信息")
    @PostMapping("/save")
    @Transactional
    public R<AbsentFromWorkEntity> save(@Validated @RequestBody AbsentFromWorkVo absentFromWorkVo) {
        Optional<StudentEntity> studentEntity = studentService.lambdaQuery().eq(StudentEntity::getSno, absentFromWorkVo.getSno()).oneOpt();
        studentEntity.orElseThrow(() -> {
            throw new RuntimeException("学号错误，没有对应的学生信息");
        });
        studentEntity.ifPresent(studentEntity1 -> {
            AbsentFromWorkEntity absentFromWorkEntity = new AbsentFromWorkEntity();
            absentFromWorkEntity.setStuId(studentEntity1.getId());
            absentFromWorkEntity.setReason(absentFromWorkVo.getReason());
            absentFromWorkEntity.setTime(new Date());
            absentFromWorkService.save(absentFromWorkEntity);
        });
        return Rb.a(AbsentFromWorkEntity.class).success().build();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新信息")
    @PutMapping("/update")
    public R<AbsentFromWorkEntity> update(@RequestBody AbsentFromWorkEntity absentFromWork) {
        absentFromWorkService.updateById(absentFromWork);
        return Rb.a(AbsentFromWorkEntity.class).success().build();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除信息")
    @DeleteMapping("/delete")
    public R<AbsentFromWorkEntity> delete(@RequestBody Long[] ids) {
        absentFromWorkService.removeByIds(Arrays.asList(ids));
        return Rb.a(AbsentFromWorkEntity.class).success().build();
    }

}
